<template>
  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :close-on-click-modal="false">
    <el-form ref="dataForm" :model="temp" label-position="left" label-width="80px"
      style="width: 80%; margin-left:50px;">
      <el-row>
        <el-col :span="24">
          <el-tabs v-model="activeTabName" type="border-card">
            <el-tab-pane label="请求头" name="table0">
              <el-table :data="serviceHeaderList" stripe border :max-height="300" style="width: 100%; margin: 15px 0;">
                <el-table-column label="序号" width="55" align="center">
                  <template slot-scope="scope">
                    <span>{{ scope.$index + 1 }}</span>
                  </template>
                </el-table-column>
                <el-table-column prop="KEY" label="键" align="center" show-overflow-tooltip />
                <el-table-column prop="VALUE" label="值" align="center" show-overflow-tooltip />
                <el-table-column prop="DESCRIPTION" label="描述" align="center" show-overflow-tooltip />
              </el-table>
            </el-tab-pane>
          </el-tabs>
        </el-col>
      </el-row>
      <el-divider content-position="left">服务信息</el-divider>
      <el-row :gutter="40">
        <el-col :span="24">
          <el-form-item label="服务名称" prop="serviceName">
            <el-input v-model="temp.serviceName" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="服务编号" prop="serviceNo">
            <el-input v-model="temp.serviceNo" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="服务类型" prop="serviceType">
            <el-select v-model="temp.serviceType" class="filter-item" placeholder="请选择服务类型" disabled>
              <el-option v-for="item in dictList('data_service_type')" :key="'type' + item.label" :label="item.label"
                :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <template v-if="temp.serviceType === '1'">
          <el-col :span="24">
            <el-form-item label="服务请求地址" prop="httpService.url">
              <el-input v-model="temp.httpService.url" placeholder="请输入服务请求地址" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="服务请求头" prop="httpService.header">
              <el-input v-model="temp.httpService.header" placeholder="请输入服务请求头，如{key:val}格式" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="服务请求参数" prop="httpService.param">
              <el-input v-model="temp.httpService.param" placeholder="请输入服务请求参数，如{key:val}格式" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="服务请求方式" prop="httpService.httpMethod">
              <el-select v-model="temp.httpService.httpMethod" placeholder="请选择请求方式" disabled>
                <el-option v-for="item in dictList('req_type')" :key="'type' + item.label" :label="item.label"
                  :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </template>
        <template v-if="temp.serviceType === '2'">
          <el-col :span="24">
            <el-form-item label="服务wsdl地址" prop="webService.wsdl">
              <el-input v-model="temp.webService.wsdl" placeholder="请输入服务wsdl地址" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="服务命名空间" prop="webService.targetNamespace">
              <el-input v-model="temp.webService.targetNamespace" placeholder="请输入服务命名空间" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="服务方法" prop="webService.method">
              <el-input v-model="temp.webService.method" placeholder="请输入服务方法" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="服务请求报文" prop="webService.soap">
              <el-input v-model="temp.webService.soap" type="textarea" placeholder="请输入服务请求报文" disabled />
            </el-form-item>
          </el-col>
        </template>
        <el-col :span="24">
          <el-form-item label="状态" prop="status">
            <el-radio disabled v-for="item in dictList('is_use')" :key="'type' + item.label" v-model="temp.status"
              :label="item.value">
              {{ item.label }}
            </el-radio>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="temp.remark" disabled />
          </el-form-item>
        </el-col>

        <el-col v-permission="['marketservice:MarketServiceIntegration:transmit']" :span="24" v-if="title !== 200">
          <el-form-item label="接口调用">
            <template>
              <pre>
                 {{ title }}
              </pre>
            </template>
          </el-form-item>
        </el-col>


      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">
        {{ $t('table.cancel') }}
      </el-button>
      <!--      <el-button type="primary" :loading="loading" @click="dialogStatus==='create'?createData():updateData()">-->
      <!--        {{ $t('table.confirm') }}-->
      <!--      </el-button>-->
    </div>
  </el-dialog>
</template>

<script>
import { getMarketServiceIntegration, getMarketServicetransmit } from '@/api/marketservice/marketServiceIntegration'


export default {
  name: 'MarketServiceIntegrationInfo',
  data() {
    return {
      title: "",
      temp: {},
      textMap: {
        info: '详情'
      },
      loading: false,
      serviceHeaderList: [],
      dialogFormVisible: false,
      activeTabName: 'table0',
      dialogStatus: ''
    }
  },
  methods: {
    getList() {
      this.$emit('refreshList')
    },
    resetTemp() {
      this.loading = false
      this.temp = {
        id: undefined,
        serviceName: undefined,
        serviceNo: undefined,
        status: undefined,
        remark: undefined,
        serviceType: undefined,
        httpService: {
          url: undefined,
          header: undefined,
          param: undefined,
          httpMethod: undefined
        },
        webService: {
          wsdl: undefined,
          targetNamespace: undefined,
          soap: undefined,
          method: undefined
        }
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.loading = true
          createMarketServiceIntegration(this.temp).then(response => {
            this.loading = false
            if (response.data.code === 0) {
              this.getList()
              this.dialogFormVisible = false
              this.$message.success(response.data.msg)
            } else {
              this.$message.error(response.data.msg)
            }
          })
        }
      })
    },
    handleInfo(id) {
      getMarketServicetransmit({
        params: {
          id: id
        }
      }).then(res => {
        if (res.data.code == 0) {
          this.title = JSON.parse(res.data.msg)
        } else {
          this.$message.error(res.data.msg)
        }
      }).catch(() => {
        this.title = 200
      })
      this.resetTemp()
      this.serviceHeaderList = []
      this.dialogStatus = 'info'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
      getMarketServiceIntegration(id).then(response => {
        if (response.data.code === 0) {
          const { data } = response.data
          this.temp = data.data
          this.apiHeader = data.header
          this.serviceHeaderList.push({ 'KEY': 'service_key', 'VALUE': data.header.serviceKey, 'DESCRIPTION': '' })
          this.serviceHeaderList.push({ 'KEY': 'secret_key', 'VALUE': data.header.secretKey, 'DESCRIPTION': '' })

        } else {
          this.dialogFormVisible = false
          this.$message.error(response.data.msg)
        }
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.loading = true
          updateMarketServiceIntegration(this.temp).then(response => {
            this.loading = false
            if (response.data.code === 0) {
              this.dialogFormVisible = false
              this.getList()
              this.$message.success(response.data.msg)
            } else {
              this.$message.error(response.data.msg)
            }
          })
        }
      })
    }
  }
}

</script>
